import axios from 'axios'
import ElementUI from 'element-ui';
// export const wsUrl = 'wss://hbt123.xyz:8086'
// export const wsUrl = 'ws://localhost:8086'
export const wsUrl = 'ws://hbt123.xyz:8086/his'

let base = '';

export const uploadFileRequest = (url, params) => {
  return new Promise((resolve, reject)=>{
    axios({
      method: 'post',
      url: `${base}${url}`,
      data: params,
      headers: {
        'Content-Type': 'multipart/form-data', 
        'token': window.localStorage.getItem('token'), 
        'origins': 'web'
      }
    }).then(res => {
      resolve(res)
    }).catch(err => {
      reject(err)
    });
  })
}

const showerr = (res)=>{
  console.debug(res)
  if(res.data.code == '403'){
    //Message 消息提示
    ElementUI.Message({
      showClose: true, 
      message: '请重新的登录',
      type: 'error'
    });
  }
}

export const myGet = (url, params)=>{
  return new Promise((resolve, reject)=>{
    axios({
      method: 'get',
      data:params,
      transformRequest: [function (data) {
        let ret = ''
        for (let it in data) {
          ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
        }
        return ret
      }],
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded', 
        'token': window.localStorage.getItem('token'), 
        'origins': 'web'
      },
      url: `${url}`
    }).then(res =>{
      showerr(res)
      resolve(res)
    }).catch(err=>{
      reject(err)
    });
  })
}


export const myDown = (url, params)=>{
  return new Promise((resolve, reject)=>{
    axios({
      method: 'post',
      data:params,
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded', 
        'token': window.localStorage.getItem('token'), 
        'origins': 'web'
      },
      responseType: 'blob',
      url: `${url}`
    }).then(res =>{
      showerr(res)
      const link = document.createElement('a');  // 创建元素
      let blob = new Blob([res.data], { type: 'application/vnd.ms-excel' });  
      link.style.display = 'none';
      link.href = URL.createObjectURL(blob);   // 创建下载的链接
      //num++
      link.setAttribute('download', '账单.xlsx');  // 给下载后的文件命名
      document.body.appendChild(link);
      link.click();  // 点击下载
      document.body.removeChild(link);  //  下载完成移除元素
      window.URL.revokeObjectURL(link.href);  // 释放掉blob对象
      resolve(res)
    }).catch(err=>{
      reject(err)
    });
  })
}

export const myPost = (url, params)=>{
  return new Promise((resolve, reject)=>{
    axios({
      method: 'post',
      data:params,
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded', 
        'token': window.localStorage.getItem('token'), 
        'origins': 'web'
      },
      url: `${url}`
    }).then(res =>{
      showerr(res)
      resolve(res)
    }).catch(err=>{
      reject(err)
    });
  })
}


export const myPostBody = (url, params)=>{
  return new Promise((resolve, reject)=>{
    axios({
      method: 'post',
      data:params,
      headers: {
        'Content-Type': 'application/json', 
        'token': window.localStorage.getItem('token'), 
        'origins': 'web'
      },
      url: `${url}`
    }).then(res =>{
      showerr(res)
      resolve(res)
    }).catch(err=>{
      reject(err)
    });
  })
}

export const myPut = (url, params)=>{
  return new Promise((resolve, reject)=>{
    axios({
      method: 'put',
      data:params,
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded', 
        'token': window.localStorage.getItem('token'), 
        'origins': 'web'
      },
      url: `${url}`
    }).then(res =>{
      showerr(res)
      resolve(res)
    }).catch(err=>{
      reject(err)
    });
  })
}

export const myPutBody = (url, params)=>{
  return new Promise((resolve, reject)=>{
    axios({
      method: 'put',
      data:params,
      headers: {
        'Content-Type': 'application/json', 
        'token': window.localStorage.getItem('token'), 
        'origins': 'web'
      },
      url: `${url}`
    }).then(res =>{
      showerr(res)
      resolve(res)
    }).catch(err=>{
      reject(err)
    });
  })
}


export const myDelete = (url, params)=>{
  return new Promise((resolve, reject)=>{
    axios({
      method: 'delete',
      data:params,
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded', 
        'token': window.localStorage.getItem('token'), 
        'origins': 'web'
      },
      url: `${url}`
    }).then(res =>{
      resolve(res)
    }).catch(err=>{
      showerr(err)
      reject(err)
    });
  })
}

export const myDeleteBody = (url, params)=>{
  return new Promise((resolve, reject)=>{
    axios({
      method: 'delete',
      data:params,
      headers: {
        'Content-Type': 'application/json', 
        'token': window.localStorage.getItem('token'), 
        'origins': 'web'
      },
      url: `${url}`
    }).then(res =>{
      resolve(res)
    }).catch(err=>{
      showerr(err)
      reject(err)
    });
  })
}